/*
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2008.
 *
 * Licensed under the Aduna BSD-style license.
 */

package edu.tju.commons.text;

import java.io.Serializable;
import java.util.Comparator;

/**
 * Comparator that compares two String representing numbers. The Strings are
 * parsed as doubles. If the parsing fails, the two Strings are compared as-is.
 * 
 * @author Herko ter Horst
 */
public class NumericStringComparator implements Comparator<String>, Serializable {

	private static final long serialVersionUID = 2136938932833734297L;

	public int compare(String oneString, String otherString) {
		int result;

		try {
			Double oneDouble = new Double(oneString);
			Double otherDouble = new Double(otherString);

			result = oneDouble.compareTo(otherDouble);
		}
		catch (NumberFormatException nfe) {
			result = oneString.compareTo(otherString);
		}

		return result;
	}
}
